﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using intelliPaper.DB;

namespace intelliPaper.Model
{
    public interface ISearchable
    {
        string Summary
        {
            get;
        }
        string Title
        {
            get;
        }
        string SearchTag
        {
            get;
        }
        int NumericSummary
        {
            get;
        }
        DateTime Date
        {
            get;
        }
    }
    /// <summary>
    /// No view model class is needed here, such as 
    /// Class QuestionViewModel X
    /// is not necessary since Question itself is modeled as a view model.
    /// </summary>
    [Serializable]
    public class Question : ISearchable, IComparable<Question>
    {
        public int NumericSummary
        {
            get;
            set;
        }
        public int CompareTo(Question compared)
        {
            //short-circuit comparison
            //only focus on configurable fields in Mgmt page
            if (compared.QuestionBody != QuestionBody)
            {
                return -1;
            }
            if (compared.QuestionType != QuestionType)
            {
                return -1;
            }
            if (compared.Possiblity != Possiblity)
            {
                return -1;
            }
            if (compared.StartDifficulty != StartDifficulty)
            {
                return -1;
            }
            if (!Helper.StringCollectionEqual(compared.TrueAnswers, TrueAnswers) ||
                !Helper.StringCollectionEqual(compared.ErrorAnswers, ErrorAnswers))
            {
                return -1;
            }
            if (KnowledgePoints.Count != compared.KnowledgePoints.Count)
            {
                return -1;
            }
            foreach (KnowledgePoint kp in KnowledgePoints)
            {
                if (!compared.KnowledgePoints.Contains(kp))
                {
                    return -1;
                }
            }
            return 0;
        }
        public Question()
        {
            ErrorAnswers = new List<string>();
            TrueAnswers = new List<string>();
            KnowledgePoints = new List<KnowledgePoint>();
            HistoryMessages = new List<HistoryMessage>();
        }
        public string Summary
        {
            get;
            set;
        }
        public string SearchTag
        {
            get
            {
                return Summary + QuestionType + StartDifficulty;
            }
        }
        public string Title
        {
            get
            {
                return this.QuestionType;
            }
        }
        public string QuestionType
        { get; set; }
        /// <summary>
        ///  题型
        /// </summary>
        public string QuestionBody
        { get; set; }
        /// <summary>
        /// 题干
        /// </summary>
        public List<string> ErrorAnswers
        { get; set; }
        /// <summary>
        ///  错误答案
        /// </summary>
        public List<string> TrueAnswers
        { get; set; }
        /// <summary>
        ///  正确答案
        /// </summary>
        public double StartDifficulty
        { get; set; }
        /// <summary>
        /// 初始难度
        /// </summary>
        //public double  ErrorRate
        //{ get; set; }
        /// <summary>
        /// 错误率
        /// </summary>
        public int UseFrequency
        { get; set; }
        /// <summary>
        /// 出题频率
        /// </summary>
        public List<KnowledgePoint> KnowledgePoints
        {
            get;
            set;
        }
        /// <summary>
        /// 知识点
        /// </summary>
        public List<HistoryMessage> HistoryMessages
        { get; set; }
        /// <summary>
        /// 一道题的历史信息
        /// </summary>
        public double Possiblity
        {
            get;
            set;

        }
        /// <summary>
        /// 一道题该出的可能性
        /// </summary>        
        public DateTime Date
        {
            get
            {
                return CreateDate;
            }
        }
        private int _ID;
        public int ID
        {
            get
            {
                return _ID;
            }
            set
            {

            }
        }
        public void SetID(int id)
        {
            this._ID = id;
        }

        public DateTime LastUsed
        { get; set; }
        /// <summary>
        ///  最后一次使用的时间
        /// </summary>
        public DateTime CreateDate
        { get; set; }
        /// <summary>
        /// 创建的时间
        /// </summary>
        /// 
        public bool IsEnabled
        {
            get;
            set;
        }

        public bool IsOpenXMLEnabled
        {
            get;
            set;
        }

        public static Question CreateShallowCopy(Question q)
        {
            return (Question)q.MemberwiseClone();
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (HistoryMessage hm in HistoryMessages)
            {
                sb.Append(hm);
                sb.Append("\n");
            }
            return string.Format("问题信息:\n题干:{0}\nID:{1}\n创建日期:{2}\n最后使用日期:{3}\n可能性:{4}\n历史消息:{5}", Summary, ID, CreateDate, LastUsed, Possiblity, sb.ToString());
        }

    }
    [Serializable]
    public class KnowledgePoint : ISearchable, IComparable<KnowledgePoint>
    {
        public int NumericSummary
        {
            get;
            set;
        }
        public int CompareTo(KnowledgePoint compared)
        {
            if (this.KnowledgePointBody != compared.KnowledgePointBody)
            {
                return -1;
            }
            if (this.Importance != compared.Importance)
            {
                return -1;
            }
            if (this.KnowledgeDifficulty != compared.KnowledgeDifficulty)
            {
                return -1;
            }
            return 0;
        }
        public static KnowledgePoint CreateShallowCopy(KnowledgePoint k)
        {
            return (KnowledgePoint)k.MemberwiseClone();
        }
        public string SearchTag
        {
            get
            {
                return Summary;
            }
        }
        public string Summary
        {
            get
            {
                return KnowledgePointBody;
            }
        }
        public string Title
        {
            get
            {
                return "重要程度： " + this.Importance.ToString();
            }
        }
        private int _ID;
        public int ID
        {
            get
            {
                return _ID;
            }
            set
            {

            }
        }
        public void SetID(int id)
        {
            this._ID = id;
        }
        public DateTime Date
        {
            get
            {
                return CreateDate;
            }
        }
        public string KnowledgePointBody
        { get; set; }
        /// <summary>
        /// 知识点
        /// </summary>
        public double Importance
        { get; set; }
        /// <summary>
        /// 重要性
        /// </summary>
        public double KnowledgeDifficulty
        { get; set; }
        /// <summary>
        /// 知识点的难度
        /// </summary>
        /// 

        public DateTime CreateDate
        {
            get;
            set;
        }

        public bool IsEnabled
        {
            get;
            set;
        }
        public bool IsOpenXMLEnabled
        {
            get;
            set;
        }

        public int UseFrequency
        {
            get;
            set;
        }
        public override string ToString()
        {
            return string.Format("知识点信息:\nID:{0}\n内容:{5}\n难度:{1}\n重要性:{2}\n建立日期:{3}\n启用:{4}", ID, KnowledgeDifficulty, Importance, CreateDate, IsEnabled, KnowledgePointBody);
        }
    }
    [Serializable]
    public class HistoryMessage
    {
        public override string ToString()
        {
            return string.Format("{0},{1}%错误", RecordDate, ErrorRate);
        }
        public static HistoryMessage CreateShallowCopy(HistoryMessage h)
        {
            return (HistoryMessage)h.MemberwiseClone();
        }
        public HistoryMessage()
        {

        }
        public int ErrorRate
        { get; set; }
        /// <summary>
        ///  历年的错误率
        /// </summary>
        /// 

        public DateTime RecordDate
        {
            get;
            set;
        }
        //public string BelongsTo
        //{
        //    get;
        //    set;
        //}
    }


    public class Paper : ISearchable, IComparable<Paper>
    {
        public int NumericSummary
        {
            get;
            set;
        }
        public string SearchTag
        {
            get
            {
                return Summary;
            }
        }
        private int _ID;

        public int ID
        {
            get
            {
                return _ID;
            }
            set { }
        }

        public void SetID(int id)
        {
            this._ID = id;
        }

        public int CompareTo(Paper compared)
        {
            if (compared.Difficulty != Difficulty)
            {
                return -1;
            }
            if (compared.Introduction != Introduction)
            {
                return -1;
            }
            if (compared.TestDate != TestDate)
            {
                return -1;
            }
            if (compared.Questions.Count != Questions.Count)
            {
                return -1;
            }
            foreach (Question q in Questions)
            {
                if (compared.Questions.Contains(q))
                {
                    return -1;
                }
            }
            return 0;
        }
        public DateTime Date
        {
            get
            {
                return TestDate;
            }
        }

        public Paper()
        {
            Questions = new List<Question>();
            KnowledgePoints = new List<KnowledgePoint>();
        }


        public List<Question> Questions
        {
            get;
            set;
        }

        public List<KnowledgePoint> KnowledgePoints
        {
            get;
            set;
        }

        public string Introduction
        {
            get;
            set;
        }

        public DateTime TestDate
        {
            get;
            set;
        }

        public DateTime LastModifyDate
        {
            get;
            set;
        }


        public double Difficulty
        {
            get;
            set;
        }

        public bool IsEnabled
        {
            get;
            set;
        }



        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("这份考卷的信息如下：\n");
            sb.Append(this.Introduction + "\n");
            sb.Append("最后一次更改于： " + this.LastModifyDate.ToShortDateString() + "\n");
            sb.Append("考试时间为： " + this.TestDate.ToShortDateString() + "\n");
            sb.Append("难度约为： " + this.Difficulty.ToString() + "\n");
            sb.Append("题目序号为（请按照序号稍后进行查找）： ");
            foreach (Question q in this.Questions)
            {
                sb.Append(q.ID + " ");
            }
            sb.Append("\n知识点序号为（请按照序号稍后进行查找）： ");
            foreach (KnowledgePoint k in this.KnowledgePoints)
            {
                sb.Append(k.ID + " ");
            }
            return sb.ToString();
            //return base.ToString();
        }

        public static Paper CreateShallowCopy(Paper p)
        {
            return (Paper)p.MemberwiseClone();
        }

        public string Summary
        {
            get
            {
                return this.Introduction;
            }
        }

        public string Title
        {
            get
            {
                return this.TestDate.ToShortDateString();
            }
        }
    }

}
